package com.hxl.tech.gateway.auth.controller;


import com.hxl.tech.gateway.auth.annotate.ApiPrivilege;
import com.hxl.tech.gateway.auth.dto.Response;
import com.hxl.tech.gateway.auth.dto.application.*;
import com.hxl.tech.gateway.auth.model.Application;
import com.hxl.tech.gateway.auth.service.ApplicationOauthService;
import com.hxl.tech.gateway.auth.service.ApplicationService;
import com.hxl.tech.gateway.auth.util.CopyUtil;
import com.hxl.tech.gateway.common.constant.AppConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * 应用 Controller 
 * @author soliddq
 * @date 2023-09-20
 */
@Api(value = "应用管理", tags = "应用管理")
@RestController
@RequiredArgsConstructor
public class ApplicationController {

    private final ApplicationService applicationService;
    private final ApplicationOauthService applicationOauthService;

    @ApiPrivilege()
    @PostMapping("gateway.auth.app.page")
    @ApiOperation(value = "分页查询应用", response = ApplicationDTO.class, responseContainer = "List")
    public Response page(@RequestBody @Validated ApplicationFindDTO findDTO) {

        return Response.createSuccess(CopyUtil.toPage(applicationService.page(findDTO), ApplicationDTO.class));
    }

    @ApiPrivilege()
    @PostMapping("gateway.auth.app.read")
    @ApiOperation(value = "读取应用", response = ApplicationDTO.class)
    public Response read(@RequestBody @Validated ApplicationReadDTO readDTO) {

        return Response.createSuccess(CopyUtil.to(applicationService.read(readDTO.getAppId()), ApplicationDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_DEVELOPER)
    @PostMapping("gateway.auth.app.save")
    @ApiOperation(value = "新增应用", response = ApplicationDTO.class)
    public Response save(@RequestBody @Validated ApplicationSaveDTO saveDTO) {

        return Response.createSuccess(CopyUtil.to(applicationService.save(CopyUtil.to(saveDTO, Application.class)), ApplicationDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_DEVELOPER)
    @PostMapping("gateway.auth.app.update")
    @ApiOperation(value = "更新应用", response = ApplicationDTO.class)
    public Response update(@RequestBody @Validated ApplicationUpdateDTO updateDTO) {

        return Response.createSuccess(CopyUtil.to(applicationService.update(CopyUtil.to(updateDTO, Application.class)), ApplicationDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.app.state.update")
    @ApiOperation(value = "启用或禁用应用", response = ApplicationDTO.class)
    public Response updateState(@RequestBody @Validated ApplicationStateUpdateDTO stateUpdateDTO) {

        return Response.createSuccess(CopyUtil.to(applicationService.updateState(stateUpdateDTO), ApplicationDTO.class));
    }

    @ApiPrivilege()
    @PostMapping("gateway.auth.app.approval.list")
    @ApiOperation(value = "查询授权列表", response = ApplicationOauthDTO.class)
    public Response listApprovalMerchantCode(@RequestBody @Validated ApplicationReadDTO readDTO) {

        return Response.createSuccess(CopyUtil.toList(applicationOauthService.findByAppId(readDTO.getAppId()), ApplicationOauthDTO.class));
    }

}
